package net.wsfcp.android.http.mvp.model;

import android.support.annotation.NonNull;
import android.util.Log;

import com.google.gson.Gson;

import net.wsfcp.android.BuildConfig;
import net.wsfcp.android.base.BaseApplication;
import net.wsfcp.android.base.BaseHttpResult;
import net.wsfcp.android.base.BaseModel;
import net.wsfcp.android.bean.requestbean.ReqFindPasswordBean;
import net.wsfcp.android.http.exception.ApiException;
import net.wsfcp.android.http.exception.ErrorType;
import net.wsfcp.android.http.exception.ExceptionEngine;
import net.wsfcp.android.http.exception.ServerException;
import net.wsfcp.android.http.mvp.subscriber.CommonSubscriber;
import net.wsfcp.android.http.mvp.subscriber.ErrorSubscriber;
import net.wsfcp.android.http.mvp.transformer.CommonTransformer;
import net.wsfcp.android.http.UrlHelper;

import java.io.InputStream;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


/**
 * Created by Shaoboyang on 2018/12/14.
 * 主要做一些数据处理呀,网路请求
 */

public class FindPasswordModel extends BaseModel {

    public void findPassword(@NonNull String username, @NonNull String email, @NonNull String code, @NonNull final FindPasswordInfoHint
            infoHint) {

        if (infoHint == null)
            throw new RuntimeException("InfoHint不能为空");


        ReqFindPasswordBean bean = new ReqFindPasswordBean(UrlHelper.getUserName(), username, email, code);//密码需要加密
        Gson gson = new Gson();
        String obj = gson.toJson(bean);
        Log.e("json数据", obj);
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), obj);
        httpService.findPassword(body).enqueue(new Callback<BaseHttpResult>() {
            @Override
            public void onResponse(Call<BaseHttpResult> call, Response<BaseHttpResult> response) {
                if (response != null && response.body() != null) {
                    if (response.body().getResultCode() == ErrorType.SUCCESS) {
                        infoHint.findPasswordSuccessInfo();
                    } else {
                        infoHint.findPasswordFailInfo(response.body().getResultMsg());
                    }
                } else {
                    infoHint.findPasswordFailInfo("失败");
                }
            }

            @Override
            public void onFailure(Call<BaseHttpResult> call, Throwable t) {
                ServerException e = ExceptionEngine.handleException(t);
                infoHint.findPasswordFailInfo(e.getMessage());
            }
        });

//                .compose(new CommonTransformer())
//                .subscribe(new CommonSubscriber(BaseApplication.getContext()) {
//                    @Override
//                    public void accept(Object o) {
//                        infoHint.findPasswordSuccessInfo();
//                    }
//
//
//                }, new ErrorSubscriber<Throwable>(BaseApplication.getContext()) {
//                    @Override
//                    public void accept(Throwable e) {
//                        if (e instanceof ServerException)
//                            infoHint.findPasswordFailInfo(((ServerException) e).message);
//
//                    }
//                });

    }


    public void getCode(@NonNull final ImageInfoHint infoHint) {
        httpService.getCodeImage().enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                infoHint.getImageCodeSuccessInfo(response.body().byteStream());
                Log.e("下载图片", "成功" + response.body().byteStream());

            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                Log.e("下载图片", "失败");

                ServerException e = ExceptionEngine.handleException(t);
                infoHint.getImageCodeFailInfo(e.getMessage());
            }
        });


    }


    //通过接口产生信息回调
    public interface FindPasswordInfoHint {
        //登录
        void findPasswordSuccessInfo();

        void findPasswordFailInfo(String str);


    }

    public interface ImageInfoHint {
        //图片验证码
        void getImageCodeSuccessInfo(InputStream inputStream);

        void getImageCodeFailInfo(String str);

    }
}
